<?php

namespace app\product\logic;

use app\common\service\UploadService;
use app\product\logic\BasicLogic;
use think\facade\Db;

class InventoryLogic extends  BasicLogic
{

    /**
     * @desc 本地数据上传OSS
     * @author luckyXiong
     * @Date   2024/03/18 10:55
     */
    public function uploadImage($params=[]){
        $doc_url   = $params['url']??'';
        //地址上传到阿里云OSS;
        $service   = new UploadService();
        $BASE_PATH = dirname(__DIR__, 1);
        $local_dir = $BASE_PATH . '/runtime/images/';
        $unique_key = uniqid();
        if (!is_dir($local_dir)) {
            mkdir($local_dir ,0777 ,true);
        }
        //存入本地地址;
        $local_dir_file = $local_dir.$unique_key.'.png';
        file_put_contents($local_dir_file, file_get_contents($doc_url));

        //上传阿里云地址;
        $ossDir   = 'images/' .date('ymd').'/'.$unique_key.'.png';
        $res      = $service->uploads($local_dir_file,$ossDir);
        //上传后清除本地数据
        unlink($local_dir_file);
        return $res['info']['url']??'';
    }



    /**
     * @desc   出入库管理列表
     * @author luckyXiong
     * @date   2024-08-02
     */
    public function getList($params=[]){
        $create_time             = getIsset($params,'create_time');
        $limit                   = getIsset($params,'limit',10);
        $where = [];
        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }
        $order = ['create_time desc'];
        $res   = Db::name('product_order_inventory')
                ->where($where)
                ->order($order)
                ->paginate($limit);
        $list = $res->items()?:[];

        $data = [
            'list'                         => $list,
            'total'                        => $res->total(),
        ];

        return  $this->jsonMessage(200,true,'获取成功',$data);
    }



    /**
     * @desc   编辑数据
     * @author luckyXiong
     * @date   2024-08-04
     */
    public function editIndex($params=[]){
           $id                     = $params['id']??'';
           $out_order_quantity     = $params['out_order_quantity']??'';
           $in_inventory_quantity  = $params['in_inventory_quantity']??'';
           $out_inventory_quantity = $params['out_inventory_quantity']??'';
           $last_buy_quantity      = $params['last_buy_quantity']??'';
           $last_arrive_quantity   = $params['last_arrive_quantity']??'';
           $long_not_send_date     = $params['long_not_send_date']??'';
           $long_have_send_date    = $params['long_have_send_date']??'';
           $not_send_order         = $params['not_send_order']??'';
           $out_send_stock         = $params['out_send_stock']??'';
           $have_send_stock        = $params['have_send_stock']??'';
           $update_time            = $params['update_time']??'';
           if(!$id){
               return  $this->jsonMessage(201,false,'ID不能为空!',null);
           }
           $update = [
                   'out_order_quantity'    =>$out_order_quantity,
                   'in_inventory_quantity' =>$in_inventory_quantity,
                   'out_inventory_quantity'=>$out_inventory_quantity,
                   'last_buy_quantity'     =>$last_buy_quantity,
                   'last_arrive_quantity'  =>$last_arrive_quantity,
                   'long_not_send_date'    =>$long_not_send_date,
                   'long_have_send_date'   =>$long_have_send_date,
                   'not_send_order'        =>$not_send_order,
                   'out_send_stock'        =>$out_send_stock,
                   'have_send_stock'       =>$have_send_stock,
                   'update_time'           =>$update_time ?:date("Y-m-d H:i:s", time()),
           ];
           Db::name('product_order_inventory')->where('id',$id)->update($update);
           return  $this->jsonMessage(200,true,'操作成功',null);
    }




    /**
     * @desc   新增数据
     * @author luckyXiong
     * @date   2024-08-04
     */
    public function addIndex($params=[]){
        $out_order_quantity     = $params['out_order_quantity']??'';
        $in_inventory_quantity  = $params['in_inventory_quantity']??'';
        $out_inventory_quantity = $params['out_inventory_quantity']??'';
        $last_buy_quantity      = $params['last_buy_quantity']??'';
        $last_arrive_quantity   = $params['last_arrive_quantity']??'';
        $long_not_send_date     = $params['long_not_send_date']??'';
        $long_have_send_date    = $params['long_have_send_date']??'';
        $not_send_order         = $params['not_send_order']??'';
        $out_send_stock         = $params['out_send_stock']??'';
        $have_send_stock        = $params['have_send_stock']??'';
        $create_time            = $params['create_time']??'';

        $insert = [
            'out_order_quantity'    =>$out_order_quantity,
            'in_inventory_quantity' =>$in_inventory_quantity,
            'out_inventory_quantity'=>$out_inventory_quantity,
            'last_buy_quantity'     =>$last_buy_quantity,
            'last_arrive_quantity'  =>$last_arrive_quantity,
            'long_not_send_date'    =>$long_not_send_date,
            'long_have_send_date'   =>$long_have_send_date,
            'not_send_order'        =>$not_send_order,
            'out_send_stock'        =>$out_send_stock,
            'have_send_stock'       =>$have_send_stock,
            'create_time'           =>$create_time ?: date("Y-m-d H:i:s", time()),
            'update_time'           =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('product_order_inventory')->insert($insert);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc   清除数据
     * @author luckyXiong
     * @date   2024-08-04
     */
    public function deleteIndex($params=[]){
           $id     = $params['id']??'';
           if(!$id){
            return  $this->jsonMessage(201,false,'ID不能为空!',null);
           }
           Db::name('product_order_inventory')->where(['id'=>$id])->delete();
           return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc   清除数据
     * @author luckyXiong
     * @date   2024-08-04
     */
    public function getReportIndex($params){
           //是否管理员
           $admin_group_id     = $params['admin_group_id']??'';
           if($admin_group_id==1){
               //每日数据;
               $where       = $map = [];
               $yesterday   = date('Y-m-d', strtotime('-1 day'));
               $time_start  = $yesterday . ' 00:00:00';
               $time_end    = $yesterday . ' 23:59:59';
               $where[]            = ['create_time','between', [$time_start, $time_end]];
               $day_info           = Db::name('product_order_inventory')->where($where)->find();
               $month_info         = Db::name('product_order_inventory')->where($where)->select();
               $month_info         = objectToArray($month_info);
               $out_order_quantity           = $day_info['out_order_quantity']??0;
               $out_order_quantity_month     = array_sum(array_column($month_info, 'out_order_quantity'));
               $out_inventory_quantity       = $day_info['out_inventory_quantity']??0;
               $out_inventory_quantity_month = array_sum(array_column($month_info, 'out_inventory_quantity'));
               $in_inventory_quantity        = $day_info['in_inventory_quantity']??0;

               //昨日采购单数，昨日到货数，最长缺货日期，最长有货日期;
               $last_buy_quantity            = $day_info['last_buy_quantity']??0;
               $last_arrive_quantity         = $day_info['last_arrive_quantity']??0;
               $long_not_send_date           = $day_info['long_not_send_date']??0;
               $long_have_send_date          = $day_info['long_have_send_date']??0;


               //本月入库数;
               $in_inventory_quantity_month  = array_sum(array_column($month_info, 'in_inventory_quantity'));


               $base_info = [
                   'out_order_quantity'                =>$out_order_quantity,
                   'out_inventory_quantity'            =>$out_inventory_quantity,
                   'out_order_quantity_month'          =>$out_order_quantity_month,
                   'out_inventory_quantity_month'      =>$out_inventory_quantity_month,
                   'in_inventory_quantity'             =>$in_inventory_quantity,
                   'in_inventory_quantity_month'       =>$in_inventory_quantity_month,
                   'last_buy_quantity'                 =>$last_buy_quantity,
                   'last_arrive_quantity'              =>$last_arrive_quantity,
                   'long_not_send_date'                =>$long_not_send_date,
                   'long_have_send_date'               =>$long_have_send_date,
               ];

               //近30天出库订单曲线图;
               $map_30       = [];
               $current_date = date('Y-m-d');
               $start_date   = date('Y-m-d', strtotime('-29 days', strtotime($current_date))) . ' 00:00:00';
               $end_date     = $current_date . ' 23:59:59';
               $map_30[]     = ['create_time','between', [$start_date, $end_date]];
               $out_30       = Db::name('product_order_inventory')->field('out_order_quantity,create_time')->where($map_30)->select();
               $out_30       = objectToArray($out_30);


               //30天出入库波浪图曲线;
               $map_30[]     =  ['create_time','between', [$start_date, $end_date]];
               $order_30     =  Db::name('product_order_inventory')->field('in_inventory_quantity,out_inventory_quantity,create_time')->where($map_30)->select();
               $order_30     =  objectToArray($order_30);

               //发货图;
               $not_send_order      = $day_info['not_send_order']??0;
               $out_send_stock      = $day_info['out_send_stock']??0;
               $have_send_stock     = $day_info['have_send_stock']??0;
               $out_send_stock_rate = ($day_info['out_send_stock']/$day_info['not_send_order'])*100;
               $have_send_stock_rate= ($day_info['have_send_stock']/$day_info['not_send_order'])*100;

               $out_send_stock_rate = getRes($out_send_stock_rate)."%";
               $have_send_stock_rate= getRes($have_send_stock_rate)."%";

               $send_stock  = [
                   'not_send_order'      => $not_send_order,
                   'out_send_stock'      => $out_send_stock,
                   'have_send_stock'     => $have_send_stock,
                   'out_send_stock_rate' => $out_send_stock_rate,
                   'have_send_stock_rate'=> $have_send_stock_rate,
               ];

               //未发订单件数;
               $spu_map   = [];
               $spu_map[] = ['not_send_quantity','>',0];
               $spu_map[] = ['all_inventory_quantity','>',0];
               $spu_info  = Db::name('product_spu_inventory')->field('spu,not_send_quantity,all_inventory_quantity')
                   ->where($spu_map)
                   ->order('not_send_quantity desc')
                   ->select()
                   ->toArray();
               $data  = [
                   'base_info' =>$base_info,
                   'out_30'    =>$out_30,
                   'order_30'  =>$order_30,
                   'send_stock'=>$send_stock,
                   'spu_info'  =>$spu_info,
               ];
           }else{
               $data  = [
                   'base_info' =>[],
                   'out_30'    =>[],
                   'order_30'  =>[],
                   'send_stock'=>[],
                   'spu_info'  =>[],
               ];
           }

          return  $this->jsonMessage(200,true,'操作成功',$data);
    }







}